package cn.hcbycloud.auth.service;


import cn.hcbycloud.auth.bo.RoleBO;
import cn.hcbycloud.auth.bo.RoleRelationBO;
import cn.hcbycloud.auth.mapper.RoleRelationMapper;
import cn.hcbycloud.auth.model.RoleRelation;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色关系表
 *
 * @author gaoheng
 * @date 2022-01-22 00:47:38
 */
@Service
@AllArgsConstructor
public class RoleRelationService extends ServiceImpl<RoleRelationMapper, RoleRelation> {
    private RoleRelationMapper roleRelationMapper;

    /**
     * 根据主体标识获取角色对象列表
     *
     * @param mainId
     * @param tenantId
     * @return
     */
    public List<RoleBO> getRolesByMainId(String mainId, String tenantId) {
        return roleRelationMapper.selectRolesByMainId(mainId,tenantId);
    }


    /**
     * 添加角色关系信息
     * @param roleRelationBo
     * @param tenantId
     */
    public void addRoleRelationInfo(RoleRelationBO roleRelationBo, String tenantId) {
        List<RoleRelation> roleRelationList = CollUtil.newArrayList();
        for (RoleBO roleBo : roleRelationBo.getRoleBos()) {
            RoleRelation roleRelation = new RoleRelation();
            roleRelation.preInsert();
            roleRelation.setMainId(roleRelationBo.getMainId());
            roleRelation.setType(roleRelationBo.getType());
            roleRelation.setRoleId(roleBo.getRoleId());
            roleRelation.setTenantId(tenantId);
            roleRelationList.add(roleRelation);
        }
        if(CollUtil.isNotEmpty(roleRelationList)) {
            this.saveBatch(roleRelationList);
        }
    }
}
